SELECT 语句
发表于 2018-3-5 15:32:05 | 分类于 SQL |
SELECT 语句用于从数据库中选取数据。
结果被存储在一个结果表中,称为结果集。
语法
SELECT column_name,column_name FROM table_name;
SELECT * FROM table_name;
示例
示例将使用 RUNOOB 样本数据库。该数据库包含一个表, "Websites" 表。
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
SELECT Column 示例
从 "Websites" 表中选取 "name" 和 "country" 列:
SELECT name,country FROM Websites;
输出结果:
mysql> SELECT name,country FROM websites;
+-----------+---------+
| name | country |
+-----------+---------+
| Google | USA |
| 淘宝 | CN |
| 菜鸟教程 | CN |
| 微博 | CN |
| Facebook | USA |
+----+-----------------+
SELECT * 示例
从 "Websites" 表中选取所有列:
SELECT * FROM Websites;
输出结果:
mysql> SELECT * FROM Websites;
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+